﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Model;

namespace Ewts.Storage
{
    public partial class Tally : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //txtInFDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                //txtOperDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                //hidOperDate.Value = txtInFDate.Value;
                HttpCookie cookie = Request.Cookies["tally"];
                
                if ( cookie != null && (cookie.Values["tallyid"] != null) && (cookie.Values["tallyid"] != "") && (cookie.Values["tallyid"] != "0"))
                {
                    hidtallyid.Value = cookie.Values["tallyid"].ToString().Trim();
                    bindDataByTallyid(hidtallyid.Value.Trim());
                   // bindDateToRepBefAndAft();           //绑定理货处理前和理货处理后列表
                }
                else
                {
                    if (Request["tallyid"] != null && Request["tallyid"].ToString().Trim() != "" && Request["tallyid"].ToString().Trim() != "0")
                    {
                        hidtallyid.Value = Request["tallyid"].ToString().Trim(); 
                        bindDataByTallyid(Request["tallyid"].ToString().Trim());
                       // bindDateToRepBefAndAft();           //绑定理货处理前和理货处理后列表

                    }
                    else if (Request["loadid"] != null && Request["loadid"].ToString().Trim() != "" && Request["loadid"].ToString().Trim() != "0")
                    {

                        string loadid = Request["loadid"].ToString().Trim();
                        bindloadInfo(loadid,0);
                        bindLoadData(loadid,0);

                    }
                    else if (Request["outid"] != null && Request["outid"].ToString().Trim() != "" && Request["outid"].ToString().Trim() != "0")
                    {
                        string outid = Request["outid"].ToString().Trim();
                        bindOutsInfo(outid,0);
                        bindOutsData(outid,0);
                    }
                }

                bindDateToRepBefAndAft();
               

            }
        }
        

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (hidtallyid.Value.Trim() != "" && hidtallyid.Value.Trim() != "0")
            {
                DataLib.BLL.Tally bll = new DataLib.BLL.Tally();
                DataLib.Model.Tally model = bll.GetModel(int.Parse(hidtallyid.Value));
                if (hidOperDate.Value.Trim() != "")
                {
                    model.OperDate = DateTime.Parse(hidOperDate.Value);
                    if (txtInFDate.Value.Trim() != "")
                        model.InFDate = DateTime.Parse(txtInFDate.Value);
                }
                if (bll.BupdateStatus(model) == true)
                {
                    DataLib.Common.MessageBox.Show(Page, "提交成功");
                    Response.Redirect("/RDLC/TallyPrintRDLC.aspx?tallyid=" + hidtallyid.Value);

                }
            }
            else
            {
                DataLib.Common.MessageBox.Show(Page, "尚未添加数据");
            }
         
                
          
        }
        
        /// <summary>
        /// Load原料理货分类数据
        /// </summary>
        /// <param name="loadid"></param>
        protected void bindLoadData(string loadid, int tallyid)
        {
         
            //原料处理后数据
            DataTable dt = new DataLib.BLL.Tally().GetBarList_F(EwtsConext_tdLoad.InnerText);
            if (dt != null && dt.Rows.Count > 0)
            {
                reptColl.DataSource = dt;
                reptColl.DataBind();
            }
            lblAfterNwt.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(loadid), 2).ToString();
  
            //原料处理前
            DataTable dt1 = new DataLib.BLL.Tally().GetUnList_F(EwtsConext_tdLoad.InnerText);
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                string insid = dt1.Rows[0]["inSid"].ToString();

                DataLib.Model.InStorage imodel = new DataLib.BLL.InStorage().GetModel(int.Parse(insid));

                DataLib.Model.Tally m = new DataLib.BLL.Tally().GetModel(tallyid);
                if (m != null && m.InFDate != null && m.InFDate.ToString().Trim() != "")
                    txtInFDate.Value = ((DateTime)m.InFDate).ToString("yyyy-MM-dd");
                reptbefore.DataSource = dt1;
                reptbefore.DataBind();

            }
                lblBeforeNwt.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(loadid),1).ToString();
            
        }

        /// <summary>
        /// 移库理货的分类数据绑定
        /// </summary>
        /// <param name="outid"></param>
        protected void bindOutsData(string outid, int tallyid)
        {
            txtWRLDate.InnerHtml = DateTime.Now.ToString();


            //原料处理后数据
             DataTable dt = new DataLib.BLL.Tally().GetBarList_Outs_F(int.Parse(outid));
            if (dt != null && dt.Rows.Count > 0)
            {
                reptColl.DataSource = dt;
                reptColl.DataBind();
            }
            
            lblAfterNwt.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(outid), 6).ToString();

            //原料处理前
            DataTable dt1 = new DataLib.BLL.Tally().GetUnList_Outs_F(int.Parse(outid));
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                string insid = dt1.Rows[0]["inSid"].ToString();

                DataLib.Model.InStorage imodel = new DataLib.BLL.InStorage().GetModel(int.Parse(insid));
                if (tallyid != 0)
                {
                    txtInFDate.Value = ((DateTime)imodel.instoragetime).ToString("yyyy-MM-dd");
                }
                else
                {
                    DataLib.Model.Tally m = new DataLib.BLL.Tally().GetModel(tallyid);
                    if (m != null && m.InFDate !=null && m.InFDate.ToString().Trim()!="")
                        txtInFDate.Value = ((DateTime)m.InFDate).ToString("yyyy-MM-dd");
                }
                reptbefore.DataSource = dt1;
                reptbefore.DataBind();

            }
            lblBeforeNwt.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(outid), 5).ToString();
            
        }

        //绑定理货处理前和理货处理后数据
        protected void bindDateToRepBefAndAft()
        {

                        
            string where = "tallyid = " + hidtallyid.Value.ToString();
            DataTable dt = new DataLib.BLL.TallyBefore().GetList(where).Tables[0];
            ReptBef.DataSource = dt;
            ReptBef.DataBind();


           
         
           
            DataTable dt1 = new DataLib.BLL.TallyAfter().GetList(where).Tables[0];
            object sumObject1 = dt1.Compute("sum(nt)", "TRUE");
 
          
            reptAft.DataSource = dt1;
            reptAft.DataBind();

            //理货处理前处理后的剩余重量
            DataLib.Model.Tally model = null;
            DataLib.BLL.Tally bll = new DataLib.BLL.Tally();
            if (hidtallyid.Value != "" && hidtallyid.Value != "0")
                model = bll.GetModel(int.Parse(hidtallyid.Value));

            if (model != null)
            {
                if (model.outid > 0)
                {
                    lblTallyBefore.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidoutid.Value), 7).ToString();
                    lblTallyAfter.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidoutid.Value), 8).ToString();
                }
                else if (model.loadid > 0)
                {
                    lblTallyBefore.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidloadid.Value), 3).ToString();
                    lblTallyAfter.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidloadid.Value), 4).ToString();
                }
            }
            else
            {
                if (hidoutid.Value !="" && hidoutid.Value != "0")
                {
                    //DataLib.Model.OutStorage omodel = new DataLib.BLL.OutStorage().GetModel(int.Parse(hidoutid.Value));
                    lblTallyBefore.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidoutid.Value), 7).ToString();
                    lblTallyAfter.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidoutid.Value), 8).ToString();
                }
                else
                {
                    //DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetModel(int.Parse(hidloadid.Value));
                    lblTallyBefore.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidloadid.Value), 3).ToString();
                    lblTallyAfter.InnerHtml = new DataLib.BLL.Tally().GetTallyWeight(int.Parse(hidloadid.Value), 4).ToString(); ; 
                }
                

            }


        }

        /// <summary>
        /// 原料理货信息
        /// </summary>
        /// <param name="loadid"></param>
        protected void bindloadInfo(string loadid,int tallyid)
        {
            DataLib.BLL.LoadNum bllload = new DataLib.BLL.LoadNum();
            DataLib.BLL.Tally blltally = new DataLib.BLL.Tally();
            DataLib.Model.LoadNum lModel = bllload.GetModel(int.Parse(loadid));

            hidloadid.Value = lModel.id.ToString();             //loadid
            EwtsConext_tdCumName.InnerText = new DataLib.BLL.Cumtomer().GetModel((int)lModel.customerid).companyname; //客户名称
            if (tallyid == 0)
            {
                txtInFDate.Value = blltally.getWrlDate((int)lModel.id, "load", "F");
                txtWRLDate.InnerHtml = blltally.getWrlDate((int)lModel.id, "load", "M");
            }
            lbclassStatus.InnerHtml = blltally.GetClassifyStatus(lModel.loadnum);
            EwtsConext_tdLoad.InnerText = lModel.loadnum;
            sumWeight.InnerHtml = lModel.totalWeight.ToString();
        }

        /// <summary>
        /// 移库理货信息
        /// </summary>
        /// <param name="outid"></param>
        protected void bindOutsInfo(string outid, int tallyid)
        {
            //绑定控件
            DataLib.Model.OutStorage oModel = new DataLib.BLL.OutStorage().GetModel(int.Parse(outid));
            DataLib.BLL.Tally blltally = new DataLib.BLL.Tally();
            hidoutid.Value = outid;
            EwtsConext_tdCumName.InnerHtml = oModel.inDepart + "伟翔";
            EwtsConext_tdLoad.InnerHtml = oModel.VLoadNum;
            sumWeight.InnerHtml = oModel.totalWeight.ToString();
            if (tallyid == 0)
            {
                txtInFDate.Value = blltally.getWrlDate((int)oModel.id, "outs", "F");
                txtWRLDate.InnerHtml = blltally.getWrlDate((int)oModel.id, "outs", "M");
            }
            lbclassStatus.InnerHtml = blltally.GetOutsInfo(int.Parse(outid), "classtatus");
           
        }

        /// <summary>
        /// 原料理货已经存在，根据原料理货ID绑定相关数据
        /// </summary>
        /// <param name="tallyid"></param>
        protected void bindDataByTallyid(string tallyid)
        {
            if (tallyid != null && tallyid.Trim() != "0")
            {
                int tid = int.Parse(tallyid);
                hidtallyid.Value = tallyid;

                DataLib.Model.Tally tmodel = new DataLib.Model.Tally();
                DataLib.BLL.Tally tbll = new DataLib.BLL.Tally();
                tmodel = tbll.GetModel(tid);
                txttallyno.Value = tmodel.tallyno;
                //加载数据
                if (tmodel.loadid != 0)
                {
                    if (tmodel.InWrlDate != null)
                        txtWRLDate.InnerHtml = ((DateTime)tmodel.InWrlDate).ToString("yyyy-MM-dd"); 
                    if (tmodel.InFDate != null)
                    {
                        txtInFDate.Value = ((DateTime)tmodel.InFDate).ToString("yyyy-MM-dd");
                        hidOperDate.Value = txtInFDate.Value;
                    }
                    //txtInFDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    //txtOperDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    //
                    bindloadInfo(tmodel.loadid.ToString(),tid
);
                    bindLoadData(hidloadid.Value, tid);      //绑定原料处理前和处理后列表
                }
                else if (tmodel.outid != null && tmodel.outid != 0)
                {
                    bindOutsInfo(tmodel.outid.ToString(),tid);
                    bindOutsData(hidoutid.Value,tid);
                }
            }
        }
    }
}